package com.school.controller.admin;

import com.school.common.ServiceResultEnum;
import com.school.entity.Carousel;
import com.school.service.CarouselService;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import com.school.util.Result;
import com.school.util.ResultGenerator;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/admin")
public class CarouselController {
    @Resource
    private CarouselService carouselService;
    @GetMapping("/carousels")
    public String carousels(HttpServletRequest request){
        request.setAttribute("path","tea_shop_carousel");
        return "admin/tea_shop_carousel";
    }
    @GetMapping("/carousels/list")
    @ResponseBody
    public Result list(@RequestParam Map<String,Object> params){
        if(StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))){
            return ResultGenerator.genFailResult("参数异常");
        }
        //分页操作
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        //创建业务层对象，根据业务层提供的业务方法做查询操作
        PageResult pageResult=carouselService.getCarouselPage(pageQueryUtil);
            return ResultGenerator.genSuccessResult(pageResult);
    }
    @PostMapping("/carousels/save")
    @ResponseBody
    public Result save(@RequestBody Carousel carousel){
        if(StringUtils.isEmpty(carousel.getCarouselUrl())||
                StringUtils.isEmpty(carousel.getRedirectUrl())||
                carousel.getCarouselRank()==null){
            return ResultGenerator.genFailResult("参数异常");
        }
        String result=carouselService.saveCarousel(carousel);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult(result);
        }
    }
    @GetMapping("/carousels/info/{id}")
    @ResponseBody
    public Result info(@PathVariable("id") Integer id){
        Carousel carousel=carouselService.getCarouselById(id);
        if(carousel!=null){
            return ResultGenerator.genSuccessResult(carousel);
        }else{
            return ResultGenerator.genFailResult(ServiceResultEnum.DB_ERROR.getResult());
        }
    }
    @PostMapping("/carousels/update")
    @ResponseBody
    public Result update(@RequestBody Carousel carousel){
        if(StringUtils.isEmpty(carousel.getCarouselUrl())||
                StringUtils.isEmpty(carousel.getRedirectUrl())||
                carousel.getCarouselRank()==null){
            return ResultGenerator.genFailResult("参数异常");
        }
        String result=carouselService.updateCarousel(carousel);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult(result);
        }
    }
    @PostMapping("/carousels/delete")
    @ResponseBody
    public Result delete(@RequestBody Integer[] ids){
       if(ids.length<1){
           return ResultGenerator.genFailResult("参数异常");
       }
        String result=carouselService.deleteCarousels(ids);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult(result);
        }
    }
}
